// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database();
const _ = db.command;
// 云函数入口函数
exports.main = async (event, context) => {
  const openid = cloud.getWXContext().OPENID
  var {check} = event
  var _updateTime = Date.now();
  if(check==0)//领取卡卷
  {
          var res =  await db.collection("jf_salecard").where({
              openid,
        }).get()
        if(res.data.length)
        {
          //优惠券++
          // var counts = res.data[0].count+1
          return await db.collection("jf_salecard").where({
              openid,
            }).update({
              data:{
                  _updateTime,
                  count:1,
                  type:0
              }
            })
        }
        else{
          //新用户
            return await db.collection("jf_salecard").add({
              data:{
                openid,
                _updateTime,
                count:1
              }
            })
        }
  }
  else{
              //查询卡卷
      var rescount = await db.collection("jf_salecard").where({
          openid,
        }).get()
      if(rescount.data.length)
      {
        var cc = rescount.data[0].count
        return cc
      }
      else{
        return false
      }
  }
  

}